package com.xnx3.banmahuishou.qishouApplet.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.elasticsearch.cluster.metadata.AliasAction.NewAliasValidator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.banmahuishou.core.util.AppUtil;
import com.xnx3.banmahuishou.qishouApplet.vo.OrderCountVO;
import com.xnx3.banmahuishou.qishouApplet.vo.QishouFractionVO;
import com.xnx3.banmahuishou.qishouApplet.vo.QishouVO;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.SessionUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.shop.core.entity.Order;

/**
 * 骑手端--我的
 * @author qyw
 */
@RestController
@RequestMapping("/qishouApplet/user")
public class UserController extends BaseController{

	@Resource
	private SqlService sqlService;
	
	
	
	/**
	 * 骑手端--查询当前用户信息
	 */
	@PostMapping("/getUserInfo.json")
	public QishouVO getUserInfo(HttpServletRequest request) {
		ActionLogUtil.insert(request,"骑手查询当前用户信息,token: "+request.getSession().getId());
		
		try {
			ActionLogUtil.insert(request, "骑手端--查询当前骑手信息-",SessionUtil.getActiveUser(request.getSession().getId()).toString());
			ActionLogUtil.insert(request, "骑手端--查询当前骑手信息-userid"+getUserId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		QishouVO vo = new QishouVO();
		int userId = getUserId();
		if (userId == 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		Qishou qishou = sqlService.findById(Qishou.class, userId);
		vo.setQishou(qishou);
		ActionLogUtil.insert(request, userId, "骑手端--查询当前骑手信息 :" + qishou.toString());
		return vo;
	}
	
	
	/**
	 * 骑手端--获取骑手经纬度
	 * @param request
	 * @param lat	纬度
	 * @param lon	经度
	 * @return
	 */
	@RequestMapping(value = "/setLatAndLon.json",method = RequestMethod.POST)
	public BaseVO setLatAndLon(HttpServletRequest request, 
			@RequestParam(value = "lat", required = false,defaultValue = "0") double lat,
            @RequestParam(value = "lon", required = false,defaultValue = "0") double lon) {
		
		int userId = getUserId();
		Qishou qishou = sqlService.findById(Qishou.class, userId);
		if (qishou == null) {
			return notLogin();
		}
		qishou.setLatitude(lat);
		qishou.setLongitude(lon);
		sqlService.save(qishou);
		ActionLogUtil.insert(request, userId, "骑手端--获取骑手经纬度 :" + qishou.toString());
		return success();
	}
	
	
	
	/**
	 * 骑手端--统计待服务订单和总订单
	 */
	@PostMapping("/orderCount.json")
	public OrderCountVO orderCount(HttpServletRequest request) {
		
		OrderCountVO vo = new OrderCountVO();
		int userId = getUserId();
		if (userId == 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		Qishou qishou = sqlService.findById(Qishou.class, userId);
		if (qishou == null) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		// 状态是待取货 且 还未超过预约时间
		String sqlString = "SELECT * FROM shop_order WHERE state = 2 AND reserve_end_time >= " + DateUtil.timeForUnix10() + " AND qishou_id = " + userId;
		List<Order> list = sqlService.findBySqlQuery(sqlString, Order.class);
		if (list != null && list.size() > 0) {
			vo.setDfwOrderCount(list.size());
			vo.setAllOrderCount(list.size() + qishou.getAllServiceNumb());
		}else {
			vo.setDfwOrderCount(0);
			vo.setAllOrderCount(qishou.getAllServiceNumb());
		}
		
		ActionLogUtil.insert(request, userId, "骑手端--统计待服务订单和总订单 :" + vo.toString());
		return vo;
	}
	
	/**
	 * 骑手端--获取骑手评价分数
	 * @param request
	 * @return 评价分数
	 */
	@RequestMapping(value = "/qishouFraction.json",method = RequestMethod.POST)
	@ResponseBody
	public QishouFractionVO qishouFraction(HttpServletRequest request) {
		QishouFractionVO vo = new QishouFractionVO();
		int userId = getUserId();
		Qishou qishou = sqlService.findById(Qishou.class, userId);
		if (qishou == null) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		//今日零点时间戳
		Integer dateZero = DateUtil.getDateZeroTime(DateUtil.timeForUnix10());
		//本月一号零点时间戳
		Integer dateBegIn = AppUtil.getMonthBegin(new Date());
		//取这个骑手 本月一日零点 到 当日零点 的订单
		String sql = "SELECT * FROM shop_order WHERE state = " + Order.STATE_EVALUATED + " AND pay_time > " + dateBegIn + " AND pay_time <= " + dateZero + " AND qishou_id = " + userId ;
		List<Order> list = sqlService.findBySqlQuery( sql , Order.class);
		int count = list.size();
		int fenshu = 0;
		int moren = 50 ;
		if (count > 0) {
			for (Order order : list) {
				fenshu = fenshu + order.getEvaluationScore();
			}
			vo.setPingfen( fenshu / count);
		}
		if (count == 0) {
			vo.setPingfen(moren);
		}
		//日志插入
		ActionLogUtil.insert(request, userId, "骑手端--获取骑手评分");
		return vo;
	}
	
	
}
